#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<limits.h>
#include<time.h>
#include<iterator>

using std::cin;
using std::cout;
using std::vector;
using std::min;
using std::pair;
using std::make_pair;
using std::queue;

const vector<int> dx {1, 0, 0, -1};
const vector<int> dy {0, 1, -1, 0};

bool is_inside(int m, int n, int x, int y) {
    return x >= 0 && x < m && y >= 0 && y < n;
}

void add_edge(vector<vector<int> > &graph, int v, int u) {
    graph[v].push_back(u);
    return;
}

int cg2int(int n, int i, int j) {
    return n * i + j;
}

void bfs(vector<vector<int> > &graph, vector<int> &distance, int n, int m, int st) {
    vector<int> used;
    used.assign(n*m, 0);
    used[st] = 1;
    distance[st] = 0;
    queue<int> q;
    q.push(st);
    while(!q.empty()) {
        int v = q.front();
        q.pop();
        for (int to : graph[v]) {
            if (!used[to]) {
                used[to] = 1;
                distance[to] = distance[v] + 1;
                q.push(to);
            }
        }
    }
    return;
}

int main() {
    srand(time(NULL));
    int m, n;
    cin >> m >> n;
    int st_x, st_y, fn_x, fn_y;
    cin >> st_x >> st_y;
    cin >> fn_x >> fn_y;
    vector<vector<int> > graph(n*m);
    vector<vector<int> > is_clear(m);
    for (int i = 0; i < m; ++i) {
        is_clear[i].resize(n);
        for (int j = 0; j < n; ++j) {
            int is_filled = 0;
            cin >> is_filled;
            is_clear[i][j] = 1 - is_filled;
        }
    }
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int index = 0; index < 4; ++index) {
                int curr_i = i + dx[index], curr_j = j + dy[index];
                if (is_inside(m, n, curr_i, curr_j)
                    && is_clear[i][j] && is_clear[curr_i][curr_j]) {
                    add_edge(graph, cg2int(n, i, j), cg2int(n, curr_i, curr_j));
                }
            }
        }
    }
    vector<int> distance;
    distance.assign(n*m, -1);
    bfs(graph, distance, n, m, cg2int(n, --st_x, --st_y));
    int min_path = distance[cg2int(n, --fn_x, --fn_y)];
    if (min_path == -1) {
        cout << "NO\n";
        return 0;
    }
    cout << min_path << "\n";
    return 0;
}
